package com.ciquan.server.service.impl;

import com.ciquan.server.bean.PushMessage;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.service.PushMessageService;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.util.URLConnUtils;
import com.ciquan.server.util.push.android.AndroidUnicast;
import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by card on 2015/3/9.
 */
@Service
public class PushMessageServiceImpl implements PushMessageService {

    private static Logger logger = Logger.getLogger(PushMessageServiceImpl.class.getName());

    private static String appkey = "5486b173fd98c5535a000792";

    private static String appMasterSecret = "viosvmtnowoxbloobw7myb48qjjw6qqa";

    @Resource
    private UserInfoService userInfoService;

    @Override
    public void sendMessage(UserInfo userInfo, PushMessage pushMessage) {
        try {
            if(StringUtils.equalsIgnoreCase("android", userInfo.getDeviceType())) {
                AndroidUnicast unicast = new AndroidUnicast();
                unicast.setAppMasterSecret(appMasterSecret);
                unicast.setPredefinedKeyValue("appkey", appkey);
                unicast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
                unicast.setPredefinedKeyValue("device_tokens", userInfo.getToken());
                unicast.setPredefinedKeyValue("ticker", "瓷圈");
                unicast.setPredefinedKeyValue("title", pushMessage.getTitle());
                unicast.setPredefinedKeyValue("text",   pushMessage.getContent());
                unicast.setPredefinedKeyValue("after_open", "go_custom");
                unicast.setPredefinedKeyValue("display_type", "notification");
                unicast.setPredefinedKeyValue("production_mode", "true");
                unicast.setPredefinedKeyValue("play_sound", "true");
                unicast.setPredefinedKeyValue("icon", "R.drawable.ci");
                unicast.setExtraField("type", pushMessage.getType());
                if(!unicast.send()) {
                    logger.error(userInfo.getMobile() + "推送消息失败");
                }
            }

            else {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                URLConnUtils.addParam(params, "token", userInfo.getToken());
                URLConnUtils.addParam(params, "alert", pushMessage.getContent());
                URLConnUtils.addParam(params, "type", pushMessage.getType());
                URLConnUtils.doPost("http://182.92.107.35/api/sendm", params);
            }
        } catch (Exception e) {
            logger.error("sendMessage error", e);
        }
    }

    @Override
    public void sendBroadcastMessage(final PushMessage pushMessage) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<String> tokens = userInfoService.tokens("");
                for(String token : tokens) {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    URLConnUtils.addParam(params, "token", token);
                    URLConnUtils.addParam(params, "alert", pushMessage.getTitle());
                    URLConnUtils.addParam(params, "type", pushMessage.getType());
                    URLConnUtils.doPost("http://182.92.107.35/api/sendm", params);
                    logger.info("send to ios token = " + token);
                }

                tokens = userInfoService.tokens("android");
                for (String token : tokens) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setToken(token);
                    sendMessage(userInfo, pushMessage);
                }
            }
        }).start();
    }
}
